package com.eqdd.yiqidian.utils;

import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseApplication;
import com.eqdd.yiqidian.bean.PushMsg;
import com.eqdd.yiqidian.bean.User;
import com.eqdd.yiqidian.bean.rebuild.CompanyInfoResultBean;
import com.eqdd.yiqidian.bean.rebuild.GroupListResultBean;
import com.eqdd.yiqidian.bean.rebuild.YueArticleBean;
import com.eqdd.yiqidian.gen.CompanyInfoResultBeanDao;
import com.eqdd.yiqidian.gen.GroupListResultBeanDao;
import com.eqdd.yiqidian.gen.PushMsgDao;
import com.eqdd.yiqidian.gen.UserDao;
import com.eqdd.yiqidian.gen.YueArticleBeanDao;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.rxbus2.RxBus;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.List;

/**
 * Created by lzh on 17-1-5.
 */

public class GreenDaoUtil {
    public static UserDao getUserDao() {
        return BaseApplication.greenDaoManager.getSession().getUserDao();
    }

    public static PushMsgDao getPushMsgDao() {
        return BaseApplication.greenDaoManager.getSession().getPushMsgDao();
    }

    public static YueArticleBeanDao getDraftDao() {
        return BaseApplication.greenDaoManager.getSession().getYueArticleBeanDao();
    }

    public static CompanyInfoResultBeanDao getCompanyDao() {
        return BaseApplication.greenDaoManager.getSession().getCompanyInfoResultBeanDao();
    }

    public static GroupListResultBeanDao getGroupDao() {
        return BaseApplication.greenDaoManager.getSession().getGroupListResultBeanDao();

    }

    public static GroupListResultBean getGroupById(String id) {
        List<GroupListResultBean> groupBeanList = getGroupDao().queryBuilder()
                .where(GroupListResultBeanDao.Properties.Groupid.eq(id))
                .list();

        return groupBeanList.size() > 0 ? groupBeanList.get(0) : null;
    }


    public static int insertGroup(GroupListResultBean bean) {
        GroupListResultBean groupById = getGroupById(bean.getGroupid());
        if (groupById != null) {
            getGroupDao().delete(groupById);
        }
        return (int) getGroupDao().insert(bean);
    }

    public static List<GroupListResultBean> getGroupAll() {
        List<GroupListResultBean> groupBeanList = getGroupDao().loadAll();
        return groupBeanList;
    }

    public static void deleteAllGroup() {
        List<GroupListResultBean> groupBeens = getGroupDao().loadAll();
        if (groupBeens == null || groupBeens.size() == 0) {
            return;
        }
        getGroupDao().deleteAll();
    }

    public static User getUser() {
        List<User> userRecs = getUserDao().queryBuilder()
                .where(UserDao.Properties.Uname.eq(SPUtil.getAccount("username", "")))
                .list();

        return userRecs.size() > 0 ? userRecs.get(0) : null;
    }

    public static void deleteAllUser() {
        getUserDao().deleteAll();
    }

    public static int insert(User user) {
        if (getUser() != null && getUser().getUname().equals(user.getUname())) {
            user.setId(getUser().getId());
            getUserDao().delete(user);
        }
        SPUtil.saveAccount("username", user.getUname());
        AppConstent.user = user;
        return (int) getUserDao().insert(user);
    }

    public static CompanyInfoResultBean getCompany() {
        List<CompanyInfoResultBean> company = getCompanyDao().queryBuilder().where(CompanyInfoResultBeanDao.Properties.Name.eq(SPUtil.getCompany("company", ""))).list();
        return company.size() > 0 ? company.get(0) : null;
    }

    public static void deleteAllCompany() {
        getCompanyDao().deleteAll();
    }

    public static int insert(CompanyInfoResultBean bean) {
        if (getCompany() != null && getCompany().getName().equals(bean.getName())) {
            bean.setId(getCompany().getId());
            getCompanyDao().delete(bean);
        }
        SPUtil.saveCompany("company", bean.getName());
        AppConstent.company = bean;
        return (int) getCompanyDao().insert(bean);
    }

    public static PushMsg getPushMsg(int code) {

        return getPushMsgDao().load((long) code);

    }

    public static int getNum(int code) {
        PushMsg pushMsg = getPushMsgDao().load((long) code);
        return pushMsg == null ? 0 : pushMsg.getNum();
    }

    public static void setNum(int code, int num) {
        setNum(code, num, null);
    }

    public static void setNum(int code, int num, String imgUrl) {
        getPushMsgDao().insertOrReplace(new PushMsg((long) code, imgUrl, num));
        RxBus.getDefault().send(code);
        if (num == 0) {
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CODE_CLEAR)
                    .params("userGuid", getUser().getGuid())
                    .params("code", code)
                    .execute(new JsonCallBack<HttpResult>() {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                        }
                    });
        }
    }

    public static void setNumNotPost(int code, int num) {
        getPushMsgDao().insertOrReplace(new PushMsg((long) code, num));
        if (num == 0) {
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CODE_CLEAR)
                    .params("userGuid", getUser().getGuid())
                    .params("code", code)
                    .execute(new JsonCallBack<HttpResult>() {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                        }
                    });
        }
    }

    public static void clearPushCode() {
        getPushMsgDao().deleteAll();
    }

    public static void saveDraft(YueArticleBean yueArticleBean) {
        getDraftDao().insertOrReplace(yueArticleBean);
    }

    public static YueArticleBean getDraft(int id) {
        return getDraftDao().load((long) id);
    }

    public static void deleteDraft(int id) {
        getDraftDao().deleteByKey((long) id);
    }
}
